/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package design;

import Dao.DangkiHP;
import Dao.Ngaysinh;
import Dao.Sinhvien;
import Entity.SinhVienEntity;
import Util.ConSQL;
import Util.Validate;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JComboBox;
import javax.swing.JFormattedTextField;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
import javax.swing.text.MaskFormatter;

/**
 *
 * @author AnhPhuong
 */
public class frmQLSinhVien extends javax.swing.JFrame {

    String maSV, tenSV, ngaysinh, quequan, email, sdt, gioitinh, socmnd, doituong, ghichu, lopSH;
    String maPB, tenPB, matkhau;
    Vector tableRecord = new Vector();
    Vector tableTitle = new Vector();
    DangkiHP dangkiHP = new DangkiHP();
    ConSQL conn = new ConSQL();
    Validate val = new Validate();
    DefaultTableModel tableModel, modelTimKiem;
    ArrayList<SinhVienEntity> arSinhvien;
    SinhVienEntity svEntity;
    Sinhvien sinhvien = new Sinhvien();

    /**
     * Creates new form frmQLSinhVien
     */
    public frmQLSinhVien() throws Exception {
        initComponents();
        this.setSize(950, 750);
        //khoi tao gia tri cho cbb
        sinhvien.cbbNgay(cbbNgay);
        sinhvien.cbbThang(cbbThang);
        sinhvien.cbbNam(cbbNam);
        sinhvien.cbbLopSH(cbblopsh);
        btnXoa.setEnabled(false);
        btnSua.setEnabled(false);
        //
         this.setLocationRelativeTo(null);
        createTitle();
        loadData();
    }

    public frmQLSinhVien(String maPB, String tenPB, String matkhau) throws Exception {
        initComponents();
        this.setSize(950, 750);
        sinhvien.cbbNgay(cbbNgay);
        sinhvien.cbbThang(cbbThang);
        sinhvien.cbbNam(cbbNam);
        sinhvien.cbbLopSH(cbblopsh);
        btnXoa.setEnabled(false);
        btnSua.setEnabled(false);
        this.maPB = maPB;
        this.tenPB = tenPB;
        this.matkhau = matkhau;
        lbMaPB.setText(maPB);
        lbTenPB.setText(tenPB);
        createTitle();
        loadData();
         this.setLocationRelativeTo(null);
    }

    public void clearTF() {
        tfMaGV.setText("");
        tfTenGV.setText("");
        tfQueQuan.setText("");
        tfSearch.setText("");
        cbbNgay.setSelectedIndex(0);
        cbbThang.setSelectedIndex(0);
        cbbNam.setSelectedIndex(0);
        cbbGioiTinh.setSelectedIndex(0);
        fmtEmail.setText("");
        fmtSoCMND.setText("");
        fmtSĐT.setText("");
        txtdoituong.setText("");
        txtghichu.setText("");
        cbblopsh.setSelectedIndex(0);
    }

    public void createTitle() {
        tableTitle.removeAllElements();
        tableTitle.addElement("Mã sinh viên");
        tableTitle.addElement("Lớp sinh hoạt");
        tableTitle.addElement("Tên sinh viên");
        tableTitle.addElement("Ngày sinh");
        tableTitle.addElement("Giới tính");
        tableTitle.addElement("Quê quán");
        tableTitle.addElement("Điện thoại");
        tableTitle.addElement("Email");
        tableTitle.addElement("Số CMND");        
        tableTitle.addElement("Đối tượng");
        tableTitle.addElement("Ghi chú");

    }

    public void creatRecord(ResultSet rs) throws SQLException, Exception {
        tableRecord.removeAllElements();
        while (rs.next()) {
            Vector data = new Vector();
            data.add(rs.getString(1).toString());
            data.add(rs.getString(2).toString());
            data.add(rs.getString(3).toString());
            data.add(rs.getString(4).toString());
            data.add(rs.getString(5).toString());
            data.add(rs.getString(6).toString());
            data.add(rs.getString(7).toString());
            data.add(rs.getString(8).toString());
            data.add(rs.getString(9).toString());
            data.add(rs.getString(10).toString());
            data.add(rs.getString(11).toString());
            tableRecord.add(data);
        }
    }

    public String layngay(JComboBox cbbNgay, JComboBox cbbThang, JComboBox cbbNam) {
        return cbbThang.getSelectedItem().toString() + "/" + cbbNgay.getSelectedItem().toString() + "/" + cbbNam.getSelectedItem().toString();
    }

    //Load dữ liệu lên bảng
    public void loadData() {
        tableModel = new DefaultTableModel();
        tableModel.setRowCount(0);

        Vector col = new Vector();
        col.addElement("Mã sinh Viên");
        col.addElement("Lớp sinh hoạt");
        col.addElement("Tên sinh Viên");
        col.addElement("Ngày Sinh");
        col.addElement("Giới tính");
        col.addElement("Quê quán");
        col.addElement("Số Điện Thoại");
        col.addElement("Email");    
        col.addElement("Số CMND");        
        col.addElement("Đối tượng");
        col.addElement("Ghi chú");
        tableModel.setColumnIdentifiers(col);
        arSinhvien = sinhvien.getAll();
        for (int i = 0; i < arSinhvien.size(); i++) {
            svEntity = arSinhvien.get(i);
            Vector row = new Vector();
            row.addElement(svEntity.getMaSV());
            row.addElement(svEntity.getMalopSH());
            row.addElement(svEntity.getHoTen());
            row.addElement(svEntity.getNgaySinh());
            row.addElement(svEntity.getGioiTinh());
            row.addElement(svEntity.getQueQuan());
            row.addElement(svEntity.getPhone());
            row.addElement(svEntity.getEmail());
            
            row.addElement(svEntity.getSCMND());
            row.addElement(svEntity.getDoiTuong());
            row.addElement(svEntity.getGhiChu());
            

            tableModel.addRow(row);
        }
        tbThongTin.setModel(tableModel);
    }

    public void loadSearch(String str, int id) {
        modelTimKiem = new DefaultTableModel();
        modelTimKiem.setRowCount(0);

        Vector col = new Vector();
       col.addElement("Mã sinh Viên");
        col.addElement("Lớp sinh hoạt");
        col.addElement("Tên sinh Viên");
        col.addElement("Ngày Sinh");
        col.addElement("Giới tính");
        col.addElement("Quê quán");
        col.addElement("Số Điện Thoại");
        col.addElement("Email");    
        col.addElement("Số CMND");        
        col.addElement("Đối tượng");
        col.addElement("Ghi chú");
        modelTimKiem.setColumnIdentifiers(col);
        if (id == 0) {
            arSinhvien = sinhvien.timKiem(str, 0);
        } else {
            arSinhvien = sinhvien.timKiem(str, 1);
        }

        for (int i = 0; i < arSinhvien.size(); i++) {
            svEntity = arSinhvien.get(i);
            Vector row = new Vector();
            row.addElement(svEntity.getMaSV());
            row.addElement(svEntity.getMalopSH());
            row.addElement(svEntity.getHoTen());
            row.addElement(svEntity.getNgaySinh());
            row.addElement(svEntity.getGioiTinh());
            row.addElement(svEntity.getQueQuan());
            row.addElement(svEntity.getPhone());
            row.addElement(svEntity.getEmail());
            row.addElement(svEntity.getQueQuan());
            row.addElement(svEntity.getSCMND());
            row.addElement(svEntity.getDoiTuong());
            row.addElement(svEntity.getGhiChu());
            
            modelTimKiem.addRow(row);
        }
        tbThongTin.setModel(modelTimKiem);
    }

    public void LoadInput() {
        maSV = tfMaGV.getText();
        tenSV = tfTenGV.getText();
        quequan = tfQueQuan.getText();
        sdt = fmtSĐT.getText();
        email = fmtEmail.getText();
        socmnd = fmtSoCMND.getText();
        gioitinh = cbbGioiTinh.getSelectedItem().toString();
        ngaysinh = layngay(cbbNgay, cbbThang, cbbNam);
        lopSH = cbblopsh.getSelectedItem().toString();
        doituong = txtdoituong.getText();
        ghichu = txtghichu.getText();

        tableModel = new DefaultTableModel();
        tableModel.setRowCount(0);
        Vector col = new Vector();
       col.addElement("Mã sinh Viên");
        col.addElement("Lớp sinh hoạt");
        col.addElement("Tên sinh Viên");
        col.addElement("Ngày Sinh");
        col.addElement("Giới tính");
        col.addElement("Quê quán");
        col.addElement("Số Điện Thoại");
        col.addElement("Email");    
        col.addElement("Số CMND");        
        col.addElement("Đối tượng");
        col.addElement("Ghi chú");
        tableModel.setColumnIdentifiers(col);
    }
    //
//    public void LoadInformotion(int iDongchon) {
//        Vector vDongchon = (Vector) tableRecord.get(iDongchon);
//        tfMaGV.setText(vDongchon.get(0).toString());
//        tfTenGV.setText(vDongchon.get(1).toString());
//        String strGioiTinh = (String) tbThongTin.getModel().getValueAt(iDongchon, 3);
//        cbbGioiTinh.setSelectedItem(strGioiTinh);
//    }
    //cac phuong thuc kiem tra input
    Ngaysinh ns = new Ngaysinh();

    public String CheckDate() {
        String cBD = "";
        int c = 0;
        int ngay = Integer.parseInt(cbbNgay.getSelectedItem().toString());
        int thang = Integer.parseInt(cbbThang.getSelectedItem().toString());
        int nam = Integer.parseInt(cbbNam.getSelectedItem().toString());
        if (ns.validDate(ngay, thang, nam) == false) {
            cBD = "Định dạng ngày  không tồn tại!\n";
            c = 1;
        }
        return cBD;
    }

    public void checkTextField(JTextField jtf, String strName) {
        if (jtf.getText().trim().equals("")) {
            JOptionPane.showMessageDialog(null, strName + " không được để trống");
            jtf.requestFocus();
        }
    }

    public void checkInputData() {
        checkTextField(tfMaGV, "Mã sinh viên");
        checkTextField(tfTenGV, "Tên sinh viên");
        checkTextField(tfQueQuan, "Quê quán");

        checkTextField(fmtSĐT, "Số điện thoại");
        checkTextField(fmtEmail, "Email");
        checkTextField(fmtSoCMND, "Số CMND");
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        lbMaPB = new javax.swing.JLabel();
        lbTenPB = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        tfMaGV = new javax.swing.JTextField();
        tfTenGV = new javax.swing.JTextField();
        tfQueQuan = new javax.swing.JTextField();
        fmtSĐT = new javax.swing.JFormattedTextField();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        cbbNgay = new javax.swing.JComboBox();
        cbbThang = new javax.swing.JComboBox();
        cbbNam = new javax.swing.JComboBox();
        cbbGioiTinh = new javax.swing.JComboBox();
        fmtSoCMND = new javax.swing.JFormattedTextField();
        fmtEmail = new javax.swing.JFormattedTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        txtdoituong = new javax.swing.JTextField();
        txtghichu = new javax.swing.JTextField();
        jLabel13 = new javax.swing.JLabel();
        cbblopsh = new javax.swing.JComboBox();
        jPanel3 = new javax.swing.JPanel();
        btnThem = new javax.swing.JButton();
        btnSua = new javax.swing.JButton();
        btnXoa = new javax.swing.JButton();
        btnNhapLai = new javax.swing.JButton();
        jPanel4 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tbThongTin = new javax.swing.JTable();
        tfSearch = new javax.swing.JTextField();
        cbbTim = new javax.swing.JComboBox();
        btnTimKiem = new javax.swing.JButton();
        btnThoat = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setBackground(new java.awt.Color(255, 255, 255));

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));
        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Thông tin phòng ban", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Times New Roman", 0, 18))); // NOI18N

        jLabel1.setText("Mã phòng ban");

        jLabel2.setText("Tên phòng ban");

        lbMaPB.setText("Mã");

        lbTenPB.setText("Tên");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2))
                .addGap(24, 24, 24)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(lbTenPB, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(lbMaPB, javax.swing.GroupLayout.DEFAULT_SIZE, 104, Short.MAX_VALUE))
                .addContainerGap(70, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(lbMaPB))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(lbTenPB))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel2.setBackground(new java.awt.Color(255, 255, 255));
        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Thông tin", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Times New Roman", 0, 18))); // NOI18N

        jLabel5.setText("Mã sinh viên :");

        jLabel6.setText("Tên sinh viên :");

        jLabel7.setText("Quê quán :");

        jLabel8.setText("Số điện thoại :");

        jLabel9.setText("Ngày sinh :");

        jLabel10.setText("Giới tính :");

        jLabel11.setText("Email :");

        jLabel12.setText("Số CMND :");

        cbbNgay.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        cbbThang.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        cbbNam.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        cbbGioiTinh.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Nam", "Nữ" }));

        try{
            fmtSoCMND = new JFormattedTextField(new MaskFormatter("#########"));
        }
        catch(Exception e){
            e.printStackTrace();
        }

        jLabel3.setText("Đối tượng: ");

        jLabel4.setText("Ghi chú: ");

        jLabel13.setText("Lớp sinh hoạt: ");

        cbblopsh.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel13)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cbblopsh, 0, 117, Short.MAX_VALUE))
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addGroup(jPanel2Layout.createSequentialGroup()
                            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel6)
                                .addComponent(jLabel7)
                                .addComponent(jLabel5)
                                .addComponent(jLabel8))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(fmtSĐT)
                                .addComponent(tfQueQuan)
                                .addComponent(tfTenGV)
                                .addComponent(tfMaGV, javax.swing.GroupLayout.DEFAULT_SIZE, 118, Short.MAX_VALUE)))
                        .addGroup(jPanel2Layout.createSequentialGroup()
                            .addComponent(jLabel3)
                            .addGap(18, 18, 18)
                            .addComponent(txtdoituong))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel9)
                    .addComponent(jLabel10)
                    .addComponent(jLabel12)
                    .addComponent(jLabel11)
                    .addComponent(jLabel4))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(cbbGioiTinh, javax.swing.GroupLayout.Alignment.LEADING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(cbbNgay, javax.swing.GroupLayout.Alignment.LEADING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGap(18, 18, 18)
                        .addComponent(cbbThang, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(cbbNam, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(txtghichu, javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(fmtEmail, javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(fmtSoCMND, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 153, Short.MAX_VALUE)))
                .addGap(78, 78, 78))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(tfMaGV, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel9)
                    .addComponent(cbbNgay, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cbbThang, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cbbNam, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(tfTenGV, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel10)
                    .addComponent(cbbGioiTinh, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(tfQueQuan, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel11)
                    .addComponent(fmtEmail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel8)
                    .addComponent(fmtSĐT, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel12)
                    .addComponent(fmtSoCMND, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 20, Short.MAX_VALUE)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel13)
                    .addComponent(cbblopsh, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jLabel4)
                    .addComponent(txtdoituong, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtghichu, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        jPanel3.setBackground(new java.awt.Color(255, 255, 255));

        btnThem.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/Button-Add-icon (1).png"))); // NOI18N
        btnThem.setText("Thêm");
        btnThem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnThemActionPerformed(evt);
            }
        });

        btnSua.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/Edit-Document-icon.png"))); // NOI18N
        btnSua.setText("Sửa");
        btnSua.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSuaActionPerformed(evt);
            }
        });

        btnXoa.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/Button-Delete-icon (1).png"))); // NOI18N
        btnXoa.setText("Xóa");
        btnXoa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnXoaActionPerformed(evt);
            }
        });

        btnNhapLai.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/Button-Refresh-icon.png"))); // NOI18N
        btnNhapLai.setText("Nhập lại");
        btnNhapLai.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNhapLaiActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addComponent(btnThem, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(58, 58, 58)
                .addComponent(btnSua, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(53, 53, 53)
                .addComponent(btnXoa, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(58, 58, 58)
                .addComponent(btnNhapLai)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGap(19, 19, 19)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnThem)
                    .addComponent(btnSua)
                    .addComponent(btnNhapLai)
                    .addComponent(btnXoa))
                .addContainerGap(23, Short.MAX_VALUE))
        );

        jPanel4.setBackground(new java.awt.Color(255, 255, 255));
        jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Danh sách sinh viên", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Times New Roman", 0, 18))); // NOI18N

        tbThongTin.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null}
            },
            new String [] {
                "Mã giáo viên", "Tên giáo viên", "Ngày sinh", "Giới tính", "Quê quán", "Số CMND"
            }
        ));
        tbThongTin.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tbThongTinMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tbThongTin);

        tfSearch.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                tfSearchKeyPressed(evt);
            }
        });

        cbbTim.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Mã Giáo Viên", "Tên Giáo Viên" }));

        btnTimKiem.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/Search-icon.png"))); // NOI18N
        btnTimKiem.setText("Tìm kiếm");
        btnTimKiem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnTimKiemActionPerformed(evt);
            }
        });

        btnThoat.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/Turn off.png"))); // NOI18N
        btnThoat.setText("Thoát");
        btnThoat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnThoatActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1))
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addGap(18, 18, 18)
                .addComponent(tfSearch, javax.swing.GroupLayout.PREFERRED_SIZE, 164, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(cbbTim, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(27, 27, 27)
                .addComponent(btnTimKiem)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(btnThoat, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(30, 30, 30))
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 140, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(26, 26, 26)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tfSearch, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cbbTim, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnTimKiem)
                    .addComponent(btnThoat))
                .addContainerGap(45, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel4, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    public void checkMail() {
        if (val.validateEmail(fmtEmail.getText())) {
        } else {
            JOptionPane.showMessageDialog(null, "Mail không hợp lê.");
            fmtEmail.setFocusable(true);
        }

    }

    public void Them() throws Exception {
        checkInputData();
        checkMail();

        maSV = tfMaGV.getText();
        tenSV = tfTenGV.getText();
        quequan = tfQueQuan.getText();
        sdt = fmtSĐT.getText();
        email = fmtEmail.getText();
        socmnd = fmtSoCMND.getText();
        lopSH = cbblopsh.getSelectedItem().toString();
        doituong = txtdoituong.getText();
        ghichu = txtghichu.getText();
        gioitinh = cbbGioiTinh.getSelectedItem().toString();
        ngaysinh = cbbThang.getSelectedItem().toString() + "/" + cbbNgay.getSelectedItem().toString() + "/" + cbbNam.getSelectedItem().toString();

        if (sinhvien.themSinhvien(maSV, lopSH, tenSV, ngaysinh, gioitinh, quequan, doituong, email, maSV, doituong, ghichu) >= 1) {
            JOptionPane.showMessageDialog(null, "Thêm mới thành công!");
            loadData();
        } else {
            JOptionPane.showMessageDialog(null, "Thêm mới thất bại!");
        }
    }

    public void Sua() throws Exception {

        checkInputData();

        maSV = tfMaGV.getText();
        tenSV = tfTenGV.getText();
        quequan = tfQueQuan.getText();
        sdt = fmtSĐT.getText();
        email = fmtEmail.getText();
        socmnd = fmtSoCMND.getText();
        lopSH = cbblopsh.getSelectedItem().toString();
        doituong = txtdoituong.getText();
        ghichu = txtghichu.getText();
        gioitinh = cbbGioiTinh.getSelectedItem().toString();
        ngaysinh = cbbThang.getSelectedItem().toString() + "/" + cbbNgay.getSelectedItem().toString() + "/" + cbbNam.getSelectedItem().toString();

        if (sinhvien.suaSinhvien(maSV, maSV, lopSH, tenSV, ngaysinh, gioitinh, quequan, email, maSV, doituong, ghichu) >= 1) {
            JOptionPane.showMessageDialog(null, "Sửa thành công");
            clearTF();
            loadData();
        } else {
            JOptionPane.showMessageDialog(null, "Sửa thất bại!");
        }

        btnXoa.setEnabled(false);
        btnSua.setEnabled(false);
//        clearTF();
    }

    public void Xoa() throws Exception {
        maSV = tfMaGV.getText();
        if (tfMaGV.equals("")) {
            JOptionPane.showMessageDialog(null, "Mã giáo viên không được để trống!");
        } else {
            if (sinhvien.xoaSinhvien(maSV) >= 1) {

                // JOptionPane.showConfirmDialog(null, "Bạn có chắc chắn muốn xóa?", "Lựa chọn", JOptionPane.YES_NO_OPTION, JOptionPane.YES_OPTION);
                JOptionPane.showMessageDialog(null, "Xóa thành công!");
                loadData();
                clearTF();
            } else {
                JOptionPane.showMessageDialog(null, "Xóa thất bại!");
            }
        }
    }
    //

    public void Tim() throws Exception {
        int iChon = cbbTim.getSelectedIndex();
        String str = tfSearch.getText();
//       loadSearch(str);
        if (iChon == 0) {
            loadSearch(str, 0);
        } else {
            loadSearch(str, 1);
        }
    }
    private void btnThemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnThemActionPerformed
        // TODO add your handling code here:
        Validate val = new Validate();
        try {
            Them();
        } catch (Exception ex) {
            Logger.getLogger(frmQuanLyGiaoVien.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_btnThemActionPerformed

    private void btnSuaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSuaActionPerformed
        // TODO add your handling code here:
        try {
            Sua();
        } catch (Exception ex) {
            Logger.getLogger(frmQuanLyGiaoVien.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_btnSuaActionPerformed

    private void btnXoaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnXoaActionPerformed
        // TODO add your handling code here:
        try {
            Xoa();
        } catch (Exception ex) {
            Logger.getLogger(frmQuanLyGiaoVien.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_btnXoaActionPerformed

    private void btnNhapLaiActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNhapLaiActionPerformed
        // TODO add your handling code here:
        clearTF();
    }//GEN-LAST:event_btnNhapLaiActionPerformed

    private void tbThongTinMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbThongTinMouseClicked
        // TODO add your handling code here:
        int iDongchon = tbThongTin.getSelectedRow();
        //mã, tên, ngày sinh, giới tính, quê quán, scmnd
        String strDay, strMonth, strYear;
        Date dNgaySinh;
        tfMaGV.setText((String) tbThongTin.getModel().getValueAt(iDongchon, 0));
        tfTenGV.setText((String) tbThongTin.getModel().getValueAt(iDongchon, 2));

        dNgaySinh = (Date) tbThongTin.getModel().getValueAt(iDongchon, 3);
        String[] strNgaySinh = dNgaySinh.toString().split("-");
        strYear = strNgaySinh[0];
        strMonth = strNgaySinh[1];
        strDay = strNgaySinh[2];
        cbbNam.setSelectedItem(Integer.parseInt(strYear));
        cbbNgay.setSelectedItem(Integer.parseInt(strDay));
        cbbThang.setSelectedItem(Integer.parseInt(strMonth));

        cbbGioiTinh.setSelectedItem((String) tbThongTin.getModel().getValueAt(iDongchon, 4));
        fmtSĐT.setText((String) tbThongTin.getModel().getValueAt(iDongchon, 6));
        tfQueQuan.setText((String) tbThongTin.getModel().getValueAt(iDongchon, 5));
        fmtSoCMND.setText((String) tbThongTin.getModel().getValueAt(iDongchon, 8));
        fmtEmail.setText((String) tbThongTin.getModel().getValueAt(iDongchon, 7));
        cbblopsh.setSelectedItem((String) tbThongTin.getModel().getValueAt(iDongchon, 1));
        txtdoituong.setText((String) tbThongTin.getModel().getValueAt(iDongchon, 9));
        txtghichu.setText((String) tbThongTin.getModel().getValueAt(iDongchon, 10));

        //fmtEmail.setText(strDay + "/" + strMonth + "/" + strYear);
        btnXoa.setEnabled(true);
        btnSua.setEnabled(true);
    }//GEN-LAST:event_tbThongTinMouseClicked

    private void tfSearchKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tfSearchKeyPressed
        // TODO add your handling code here:
        if (evt.getKeyCode() == 10) {
            btnTimKiem.doClick();
        }
    }//GEN-LAST:event_tfSearchKeyPressed

    private void btnTimKiemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnTimKiemActionPerformed
        // TODO add your handling code here:
        try {
            Tim();
        } catch (Exception ex) {
            Logger.getLogger(frmQuanLyGiaoVien.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_btnTimKiemActionPerformed

    private void btnThoatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnThoatActionPerformed
        // TODO add your handling code here:
        new FormLogin(this, rootPaneCheckingEnabled).setVisible(true);
        this.setVisible(false);
    }//GEN-LAST:event_btnThoatActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(frmQLSinhVien.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(frmQLSinhVien.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(frmQLSinhVien.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(frmQLSinhVien.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    new frmQLSinhVien().setVisible(true);
                } catch (Exception ex) {
                    Logger.getLogger(frmQLSinhVien.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnNhapLai;
    private javax.swing.JButton btnSua;
    private javax.swing.JButton btnThem;
    private javax.swing.JButton btnThoat;
    private javax.swing.JButton btnTimKiem;
    private javax.swing.JButton btnXoa;
    private javax.swing.JComboBox cbbGioiTinh;
    private javax.swing.JComboBox cbbNam;
    private javax.swing.JComboBox cbbNgay;
    private javax.swing.JComboBox cbbThang;
    private javax.swing.JComboBox cbbTim;
    private javax.swing.JComboBox cbblopsh;
    private javax.swing.JFormattedTextField fmtEmail;
    private javax.swing.JFormattedTextField fmtSoCMND;
    private javax.swing.JFormattedTextField fmtSĐT;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lbMaPB;
    private javax.swing.JLabel lbTenPB;
    private javax.swing.JTable tbThongTin;
    private javax.swing.JTextField tfMaGV;
    private javax.swing.JTextField tfQueQuan;
    private javax.swing.JTextField tfSearch;
    private javax.swing.JTextField tfTenGV;
    private javax.swing.JTextField txtdoituong;
    private javax.swing.JTextField txtghichu;
    // End of variables declaration//GEN-END:variables
}
